﻿using System;

namespace nGoogleChart
{
    public enum Encoding
    {
        [StringValue("t")]
        TextEncoding,
        [StringValue("t")]
        TextEncodingWithDataScaling,
        [StringValue("s")]
        SimpleEncoding,
        [StringValue("e")]
        ExtendedEncoding
    }

    public enum LabelLocation
    {
        [StringValue("x")]
        X,
        [StringValue("y")]
        Y,
        [StringValue("t")]
        Top,
        [StringValue("r")]
        Right
    }

    public static class EnumHelper
    {
        /// <summary>
        /// Will get the string value for a given enums value, this will
        /// only work if you assign the StringValue attribute to
        /// the items in your enum.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetStringValue(this Enum value)
        {
            // Get the type
            var type = value.GetType();

            // Get fieldinfo for this type
            var fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            var attribs = fieldInfo.GetCustomAttributes(
                              typeof (StringValueAttribute), false) as StringValueAttribute[];

            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].StringValue : null;
        }
    }
}